package com.ledi.guns.modular.ledipay.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ledi.guns.common.exception.AppCheckException;
import com.ledi.guns.common.persistence.dao.HelpCenterMapper;
import com.ledi.guns.common.persistence.dao.HelpTypeMapper;
import com.ledi.guns.common.persistence.dao.TypeMapper;
import com.ledi.guns.common.persistence.dao.UserMapper;
import com.ledi.guns.common.persistence.model.HelpCenter;
import com.ledi.guns.common.persistence.model.HelpType;
import com.ledi.guns.common.persistence.model.Type;
import com.ledi.guns.common.persistence.model.User;
import com.ledi.guns.common.persistence.model.vo.HelpVo;
import com.ledi.guns.common.persistence.model.vo.TypeVo;
import com.ledi.guns.core.shiro.ShiroKit;
import com.ledi.guns.core.shiro.ShiroUser;
import com.ledi.guns.core.util.SystemConstans;
import com.ledi.guns.modular.ledipay.controller.req.help.HelpCenterAdd;
import com.ledi.guns.modular.ledipay.controller.req.help.QuestionReq;
import com.ledi.guns.modular.ledipay.controller.req.help.TypeAdd;
import com.ledi.guns.modular.ledipay.controller.req.help.TypeReq;
import com.ledi.guns.modular.ledipay.service.HelpCenterService;

/**
 * 文件名称： com.ledi.guns.modular.ledipay.service.impl.HelpCenterServiceImpl.java<br/>
 * 初始作者： 钟小平<br/>
 * 创建日期： 2018年9月13日<br/>
 * 功能说明： 帮助中心service实现<br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (乐迪教育科技有限公司-幼儿事业部) 2017-2018.All rights reserved.<br/>
 */
@Service
public class HelpCenterServiceImpl implements HelpCenterService {

	@Resource
	private TypeMapper typeMapper;

	@Resource
	private UserMapper userMapper;

	@Resource
	private HelpTypeMapper helpTypeMapper;

	@Resource
	private HelpCenterMapper helpCenterMapper;

	/**
	 * 方法描述: [类型列表.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-上午11:06:19<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public Page<TypeVo> pageTypeList(Page<TypeVo> page, TypeReq query) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}
		List<TypeVo> list = typeMapper.pageList(page, query.getSearch());
		Integer selectCount = helpTypeMapper.selectCount(new EntityWrapper<HelpType>().eq("is_nomal", SystemConstans.YES));

		if (StringUtils.isNotBlank(query.getTitle())) {
			selectCount = helpTypeMapper.selectCountByTitleAndIsNomal(query.getTitle(), SystemConstans.YES);
		}
		list.stream().findFirst().get().setHelpCount(selectCount);
		page.setRecords(list);
		return page;
	}

	/**
	 * 方法描述: [添加帮助类型.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-上午11:35:58<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	@Transactional
	public void addType(TypeAdd typeAdd) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}

		if (typeAdd.getId() == null) {

			Type old = new Type();
			old.setName(typeAdd.getName());
			old.setIsDelete(SystemConstans.NO);
			Type oldOne = typeMapper.selectOne(old);
			if (oldOne != null) {
				throw new AppCheckException("help.type.exit.code", "help.type.exit.message", typeAdd.getName());
			}
			Type entity = new Type();
			BeanUtils.copyProperties(typeAdd, entity);
			entity.setIsDelete(SystemConstans.NO);
			entity.setCreateTime(new Date());
			entity.setCreatorId(Long.parseLong(oneUser.getId() + ""));
			entity.setUpdateTime(new Date());
			entity.setUpdaterId(Long.parseLong(oneUser.getId() + ""));
			entity.setVersionNo(SystemConstans.NO);
			typeMapper.insert(entity);
		} else {

			Type oldOne = typeMapper.selectById(typeAdd.getId());
			Map<String, Object> typeParam = new HashMap<String, Object>();
			typeParam.put("name", typeAdd.getName());
			typeParam.put("is_delete", SystemConstans.NO);
			List<Type> typelist = typeMapper.selectByMap(typeParam);
			if (CollectionUtils.isNotEmpty(typelist) && !oldOne.getName().equals(typeAdd.getName())) {
				throw new AppCheckException("help.type.exit.code", "help.type.exit.message", typeAdd.getName());
			}
			Type entity = new Type();
			BeanUtils.copyProperties(typeAdd, entity);
			entity.setIsDelete(SystemConstans.NO);
			entity.setUpdateTime(new Date());
			entity.setUpdaterId(Long.parseLong(oneUser.getId() + ""));
			typeMapper.updateById(entity);

		}
	}

	/**
	 * 方法描述: [删除分类并归类至未分类.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-下午1:52:41<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	@Transactional
	public void removeType(Long id) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}
		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("type_id", id);
		List<HelpType> selectByMap = helpTypeMapper.selectByMap(columnMap);

		if (CollectionUtils.isNotEmpty(selectByMap)) {
			HelpType entity = new HelpType();
			entity.setTypeId(SystemConstans.TYPE_NO_ID);
			helpTypeMapper.update(entity, new EntityWrapper<HelpType>().where("type_id", id));
		}
		typeMapper.deleteById(id);

	}

	/**
	 * 方法描述: [帮助中心问题列表.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-下午3:01:09<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public Page<HelpVo> pageHelpList(Page<HelpVo> page, QuestionReq query) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}
		List<HelpVo> list = helpCenterMapper.pageList(page, query.getSearch());
		page.setRecords(list);
		return page;

	}

	/**
	 * 方法描述: [新增修改问题.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-下午3:29:52<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	@Transactional
	public void addHelp(HelpCenterAdd helpCenterAdd) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}

		if (helpCenterAdd.getId() == null) {

			HelpCenter old = new HelpCenter();
			old.setTitle(helpCenterAdd.getTitle());
			old.setIsDelete(SystemConstans.NO);
			HelpCenter oldOne = helpCenterMapper.selectOne(old);
			if (oldOne != null) {
				throw new AppCheckException("help.question.exit.code", "help.question.exit.message", helpCenterAdd.getTitle());
			}
			HelpCenter entity = new HelpCenter();
			BeanUtils.copyProperties(helpCenterAdd, entity);
			entity.setIsDelete(SystemConstans.NO);
			entity.setCreateTime(new Date());
			entity.setCreatorId(Long.parseLong(oneUser.getId() + ""));
			entity.setUpdateTime(new Date());
			entity.setUpdaterId(Long.parseLong(oneUser.getId() + ""));
			entity.setVersionNo(SystemConstans.NO);
			helpCenterMapper.insert(entity);
			HelpType helpType = new HelpType();
			helpType.setHelpId(entity.getId());
			Long typeId = helpCenterAdd.getTypeId();
			Type type = typeMapper.selectById(typeId);
			if (type == null) {
				throw new AppCheckException("help.type.notExit.code", "help.type.notExit.message");
			}
			helpType.setTypeId(typeId);
			helpType.setIsNomal(helpCenterAdd.getIsNomal());
			helpTypeMapper.insert(helpType);

		} else {

			HelpCenter oldOne = helpCenterMapper.selectById(helpCenterAdd.getId());
			Map<String, Object> typeParam = new HashMap<String, Object>();
			typeParam.put("title", helpCenterAdd.getTitle());
			typeParam.put("is_delete", SystemConstans.NO);
			List<HelpCenter> typelist = helpCenterMapper.selectByMap(typeParam);
			if (CollectionUtils.isNotEmpty(typelist) && !oldOne.getTitle().equals(helpCenterAdd.getTitle())) {
				throw new AppCheckException("help.type.exit.code", "help.type.exit.message", helpCenterAdd.getTitle());
			}
			HelpCenter entity = new HelpCenter();
			BeanUtils.copyProperties(helpCenterAdd, entity);
			entity.setIsDelete(SystemConstans.NO);
			entity.setUpdateTime(new Date());
			entity.setUpdaterId(Long.parseLong(oneUser.getId() + ""));
			helpCenterMapper.updateById(entity);
			Map<String, Object> columnMap = new HashMap<String, Object>();
			columnMap.put("help_id", helpCenterAdd.getId());
			helpTypeMapper.deleteByMap(columnMap);
			HelpType helpType = new HelpType();
			helpType.setHelpId(entity.getId());
			Long typeId = helpCenterAdd.getTypeId();
			Type type = typeMapper.selectById(typeId);
			if (type == null) {
				throw new AppCheckException("help.type.notExit.code", "help.type.notExit.message");
			}
			helpType.setTypeId(typeId);
			helpType.setIsNomal(helpCenterAdd.getIsNomal());
			helpTypeMapper.insert(helpType);
		}
	}

	/**
	 * 方法描述: [移除问题.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年9月13日-下午4:02:15<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public void removeHelp(HelpCenterAdd helpCenterAdd) {

		ShiroUser currentUser = ShiroKit.getUser();
		User oneUser = userMapper.selectById(currentUser.getId());
		if (!oneUser.getStatus().equals(SystemConstans.YES)) {
			throw new AppCheckException("school.user.notActive.code", "school.user.notActive.message");
		}

		HelpCenter one = new HelpCenter();
		one.setId(helpCenterAdd.getId());
		one.setVersionNo(helpCenterAdd.getVersionNo());
		HelpCenter selectById = helpCenterMapper.selectOne(one);
		if (selectById == null) {
			return;
		}
		HelpCenter entity = new HelpCenter();
		entity.setId(helpCenterAdd.getId());
		entity.setVersionNo(helpCenterAdd.getVersionNo());
		entity.setIsDelete(SystemConstans.YES);
		entity.setUpdateTime(new Date());
		entity.setUpdaterId(Long.parseLong(oneUser.getId() + ""));
		helpCenterMapper.updateById(entity);

		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("help_id", helpCenterAdd.getId());
		helpTypeMapper.deleteByMap(columnMap);

	}

	/**
	 * 方法描述: [问题详情.]<br/>
	 * 初始作者: 钟小平<br/>
	 * 创建日期: 2018年10月10日-上午11:25:14<br/>
	 * 开始版本: 2.0.0<br/>
	 * =================================================<br/>
	 * 修改记录：<br/>
	 * 修改作者 日期 修改内容<br/>
	 * ================================================<br/>
	 */
	@Override
	public HelpVo findById(Long id) {

		HelpCenter center = new HelpCenter();
		center.setId(id);
		center.setIsDelete(SystemConstans.NO);
		HelpCenter hc = helpCenterMapper.selectOne(center);

		if (hc == null) {
			throw new AppCheckException("help.question.exit.code", "help.question.exit.message");
		}
		HelpVo vo = new HelpVo();
		BeanUtils.copyProperties(hc, vo);
		HelpType entity = new HelpType();
		entity.setHelpId(id);

		if (hc != null) {

			String sourceZh = null;
			switch (hc.getSource()) {
				case "0":
					sourceZh = SystemConstans.SOURCE_SCHOOL;
					break;
				case "1":
					sourceZh = SystemConstans.SOURCE_CUSTOMER;
					break;
				case "0,1":
					sourceZh = SystemConstans.SOURCE_SCHOOL + "|" + SystemConstans.SOURCE_CUSTOMER;
					break;
				default:
					break;
			}
			vo.setSourceZh(sourceZh);
		}
		HelpType selectOne = helpTypeMapper.selectOne(entity);
		if (selectOne != null) {
			vo.setTypeId(selectOne.getTypeId());
			Type type = typeMapper.selectById(selectOne.getTypeId());
			if (type != null) {
				vo.setTypeName(type.getName());
			}
			vo.setIsNomal(selectOne.getIsNomal());
			String zh = null;
			switch (selectOne.getIsNomal()) {
				case 0:
					zh = SystemConstans.NO_ZH;
					break;
				case 1:
					zh = SystemConstans.YES_ZH;
					break;
				default:
					break;
			}
			vo.setIsNomalZh(zh);
		}
		User updateUser = userMapper.selectById(hc.getUpdaterId());
		if (updateUser != null) {
			vo.setUpdateName(updateUser.getName());
		}
		return vo;
	}
}
